package com.xone.xapp;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FooterBar extends LinearLayout {

	public FooterBar(Context context) {
		super(context);
	}
	
	public FooterBar(final Context context, AttributeSet attrs) {
		super(context,attrs);		
		setOrientation(HORIZONTAL);
		setBackgroundColor(getResources().getColor(android.R.color.transparent));

		LayoutInflater inflater = (LayoutInflater) 
		context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.footer_bar, this);
		
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FooterBar);
		String option = typedArray.getString(R.styleable.FooterBar_tab_id);
		typedArray.recycle();
		
		String resourceId = context.getPackageName() + ":id/"+option;
		final int optionId = getResources().getIdentifier(resourceId,null,null);        		
		TextView currentOption = (TextView) findViewById(optionId);
		currentOption.setBackgroundColor(getResources().getColor(R.color.footer_focus_color));
		currentOption.setTextColor(getResources().getColor(android.R.color.black));
		currentOption.requestFocus(optionId);
		currentOption.setCompoundDrawablesWithIntrinsicBounds(0, ContextUtils.getResourceId(context, option + "_p", "drawable"), 0, 0);
		currentOption.setFocusable(false);
		currentOption.setClickable(false);
		
		TextView tab1 = (TextView) findViewById(R.id.tab1);
		tab1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (R.id.tab1 == optionId) {
					return;
				}
				Intent intent = new Intent(context, GuideActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
		});
		
		TextView tab2 = (TextView) findViewById(R.id.tab2);
		tab2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (R.id.tab2 == optionId) {
					return;
				}
				Intent intent = new Intent(context, MarketActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
		});
		
		TextView tab3 = (TextView) findViewById(R.id.tab3);
		tab3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (R.id.tab3 == optionId) {
					return;
				}
				Intent intent = new Intent(context, AssistantActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
		});
		
		TextView tab4 = (TextView) findViewById(R.id.tab4);
		tab4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (R.id.tab4 == optionId) {
					return;
				}
				Intent intent = new Intent(context, UserCenterActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
		});
	}

}
